#include("/front/family/_layout.html")
#@layout()
#define layoutImportcss()
    <link rel="stylesheet" href="/front/css/sheet.css">
    <link rel="stylesheet" href="/front/css/popup.css">
    <link rel="stylesheet" href="/front/css/smart-select.css">
#end
#define layoutHead()
	<title>家长资料</title>
#end
#define layoutHome()
<div class="navbar">
	<div class="navbar-inner toolbstyle">
		<div class="left">
			<a id="a_back" class="link external back" href="javascript:window.location.href='/front/family/classes'">
				<i class="icon icon-back"></i>
			</a>
		</div>
		<div class="title" id="navbar_title">家长资料</div>
		<div class="right"></div>
	</div>
</div>

<div class="page-content patriarch-page-content">

	<div class="list">
		<ul>
			<li>
				<div class="item-content">
					<div class="item-inner">
						<span class="head-portrait head-portrait-big"><img id="img_user_head" src="#"></span>
						<a id="a_user_head" class="item-value user-head external" style="text-align: right; position: absolute; right:30px;">修改头像
							<input type="file" id="fileselector" name="photoselector" multiple style="width:0px; height: 0px;">
						</a>
					</div>
				</div>
			</li>
			<li>
				<a class="item-link smart-select smart-select-init" data-open-in="sheet" data-close-on-select="true" data-sheet-close-link-text="完成">
					<select id="class_id" name="class_id">
						<option value="">请选择</option>
						#for(rd : listClass)
						<option value="#(rd.getInt("class_id"))" #if(rd.getInt("class_id")==login_parent_rd.getInt("class_id")) selected="selected" #end>#(rd.getStr("class_name"))</option>
						#end
					</select>
					<div class="item-content">
						<div class="item-inner">
							<div class="item-title">请选择班级</div>
						</div>
					</div>
				</a>
			</li>
			<li>
				<a class="item-link smart-select smart-select-init" data-open-in="sheet" data-close-on-select="true" data-sheet-close-link-text="完成">
					<select id="student_id" name="student_id">
						<option value="">请选择</option>
						#for(rd : listStudents)
						<option value="#(rd.getInt("student_id"))" #if(rd.getInt("student_id")==login_parent_rd.getInt("student_id")) selected="selected" #end>#(rd.getStr("student_name"))</option>
						#end
					</select>
					<div class="item-content">
						<div class="item-inner">
							<div class="item-title">请选择孩子</div>
						</div>
					</div>
				</a>
			</li>
			<li>
				<a class="item-link smart-select smart-select-init" data-open-in="sheet" data-close-on-select="true" data-sheet-close-link-text="完成">
					<select id="kindred_id" name="kindred_id">
						<option value="">请选择</option>
						#for(rd : listKindred)
						<option value="#(rd.getInt("kindred_id"))" #if(rd.getInt("kindred_id")==login_parent_rd.getInt("kindred_id")) selected="selected" #end>#(rd.getStr("kindred_name"))</option>
						#end
					</select>
					<div class="item-content">
						<div class="item-inner">
							<div class="item-title">家长称谓</div>
						</div>
					</div>
				</a>
			</li>
			<li>
				<a class="item-link popup-open" data-popup=".popup-patriarch-name">
					<div class="item-content">
						<div class="item-inner">
							<div class="item-title">家长姓名</div>
							<div class="item-value parent_name">#(login_parent_rd.getStr("parent_name") ??)</div>
						</div>
					</div>
				</a>
			</li>
		</ul>
		<div><button id="btn_all_sub" class="button button-fill botton-width" style="margin-top: 10px;">提交更新</button></div>
	</div>
</div>

	<!-- 弹出浮层页面 -->
	<div class="popup popup-patriarch-name">
		<div class="block">
			<p class="popup-input"><input type="text" name="parent_name" id="parent_name" placeholder="输入家长姓名"></p>
			  <!-- Close Popup -->
			<p><a class="link col button button-fill" id="popup-patriarch-submit" href="#">确定</a></p>
			<p><a class="link col button button-fill popup-close"  href="#">取消</a></p>
		</div>
	</div>
	<div class="popup popup-student-name">
		<div class="block">
			<p class="popup-input"><input type="text" name="student_name" id="student_name" placeholder="输入孩子姓名"></p>
			<p class="popup-input"><input type="text" name="student_no" id="student_no" placeholder="输入孩子学号"></p>
			  <!-- Close Popup -->
			<p><a class="link col button button-fill" id="popup-student-submit" href="#">确定</a></p>
			<p><a class="link col button button-fill popup-close"  href="#">取消</a></p>
		</div>
	</div>
#end
#define layoutImportjs()
    <script type="text/javascript" src="/front/js/sheet.js"></script>
    <script type="text/javascript" src="/front/js/popup.js"></script>
    <script type="text/javascript" src="/front/js/smart-select.js"></script>
#end
#define layoutScript()
<script type="text/javascript">
	var img_user_head_url = '#(login_user_record.getStr("user_img") ?? "/front/img/user.png")';
	$$('#img_user_head').attr('src', getZoomFileUrlwh(img_user_head_url, 100, 100));
	
	var needmore = #(attr_needmore ?? "0");
	var post_url = "/front/family/parent/update";
	
	function checkNeedMore(){
		if (!isBlank(needmore) && needmore == 1){
			needmore++;
			$$('#a_back').attr('href','/front/family/classes').hide();
			$$('#navbar_title').html('&nbsp;&nbsp;&nbsp;&nbsp;完善家长信息');
			$$('#nav_title').text('绑定班级');
			//alert('您的资料不全，请先完善资料！');
		}
	}
	
	
	$$('#btn_all_sub').on("click", function(){
		var class_id = $$('#class_id').val();
		var student_id = $$('#student_id').val();
		var kindred_id = $$('#kindred_id').val();
		var parent_name = $$('.parent_name').text();
		if (isBlank(class_id)){ app.dialog.alert('请选择班级！', ''); }
		else if (isBlank(student_id)){ app.dialog.alert('请选择学生！', ''); }
		else if (isBlank(kindred_id)){ app.dialog.alert('请选择家长称谓！', ''); }
		else if (isBlank(parent_name)){ app.dialog.alert('请输入家长姓名！', ''); }
		else{
			if (!isBlank(needmore) && needmore == 1){
				window.location.href = '/front/family/classes';
			}else{
				app.dialog.confirm('更新成功！', '', function(){
					$$("#a_back").click();
				});
			}
		}
	});
	
	$$('#a_user_head').on("click", function(){
		$$("#fileselector").click();
	});
	
	$$("#fileselector").on("change", function(e){
		var file = e.target.files[0],
        reader = new FileReader();
		reader.readAsDataURL(file);
		reader.onload = function (ev) {
			if (checkUploadFile(file, 'images')){
				photo = ev.target.result //获取图片内容：base64字符串
				var formdata = { filename: file.name, 
								 filesize: file.size, 
								 filetype: '.'+file.type.split("/")[1], 
								 base64_data: photo };
								 
				sendPost('/front/family/parent/upload_userhead', formdata, function(res){
					//console.log(res);
					if (res.code == 0){
						$$('.head-portrait-big img').attr("src", photo);
					}else{
						app.dialog.alert(res.msg,'');
					}
				});
			}
		};
	});
	
	$$('#kindred_id').on('change', function(){
		var value = $$("#kindred_id").val();
		sendPost(post_url, { kindred_id: value }, function(res){
			if (res.code == 200){
				//
			}else{
				app.dialog.alert(res.msg,'');
			}
		});
	});
	 
	$$('#class_id').on('change', function(){
		var value = $$("#class_id").val();
		sendPost(post_url, { class_id: value }, function(res){
			if (res.code == 200){
				var shtml = '<option value="">请选择</option>';
				if (res.hasOwnProperty('listStudents')){
					for(var i=0; i<res.listStudents.length; i++){
						var jdata = res.listStudents[i];
						shtml += '<option value="'+jdata.student_id+'">'+jdata.student_name+'</option>';
					}
				}
				console.log(shtml)
				$$('#student_id').html(shtml);
			}else{
				app.dialog.alert(res.msg,'');
			}
		});
	});
	
	$$('#student_id').on('change', function(){
		var value = $$("#student_id").val();
		sendPost(post_url, { student_id: value }, function(res){
			if (res.code == 200){
				//
			}else{
				app.dialog.alert(res.msg,'');
			}
		});
	});	
	
	
	$$("#popup-patriarch-submit").on("click", function(){
	    var value = $$("#parent_name").val();
		sendPost(post_url, { parent_name: value }, function(res){
			if (res.code == 200){
				$$(".parent_name").html(value);
				app.popup.close($$('.popup-patriarch-name'));
			}else{
				app.dialog.alert(res.msg,'');
			}
		});
	});
	
	$$("#popup-student-submit").on("click", function(){
	    var value = $$("#student_name").val();
		var sno = $$("#student_no").val();
		var classid = $$("#class_id").val();
		sendPost(post_url, { cid: classid, student_name: value, student_no: sno }, function(res){
			if (res.code == 200){
				$$(".student_name").html(value);
				app.popup.close($$('.popup-student-name'));
			}else{
				app.dialog.alert(res.msg,'');
			}
		});
	});
	checkNeedMore();
</script>
#end